<?php

namespace app\admin\model\course;

use app\admin\model\user\Student;
use think\Model;


class OrderSign extends Model
{
    // 表名
    protected $name = 'course_order_sign';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;
    // 追加属性
    protected $append = [
        'status_text',
    ];
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getMiniAppStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    const STATUS_LACK = -1;

    const STATUS_SIGN = 1; // 前台签到
    const STATUS_LEAVE = 2; //请假
    const STATUS_ACTIVITY = 3; // 活动
    const STATUS_ADMIN = 4; // 后台消耗
    const STATUS_ADMIN_SIGN = 5; // 后台签到
    public function getAdminStatusList(){
        return [
            self::STATUS_ADMIN_SIGN => "后台签到",
//            self::STATUS_LACK => "缺卡",
            self::STATUS_LEAVE => "请假",
            self::STATUS_ACTIVITY => "活动",
        ];
    }
    public function getMiniAppStatusList(){
        return [
            self::STATUS_SIGN => "签到",
            self::STATUS_ADMIN_SIGN => "后台签到",
//            self::STATUS_LACK => "缺卡",
            self::STATUS_LEAVE => "请假",
            self::STATUS_ACTIVITY => "活动",
            self::STATUS_ADMIN => "",
        ];
    }
    public function getStatusList(){
        return [
            self::STATUS_SIGN => "签到",
            self::STATUS_ADMIN_SIGN => "后台签到",
//            self::STATUS_LACK => "缺卡",
            self::STATUS_LEAVE => "请假",
            self::STATUS_ACTIVITY => "活动",
            self::STATUS_ADMIN => "后台消耗",
        ];
    }
    public function getStatusText($status){
        $list = $this->getStatusList();
        if(isset($list[$status])){
            return $list[$status];
        }else{
            return "";
        }

    }

    public function school()
    {
        return $this->belongsTo('app\admin\model\user\School', 'school_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function teacher()
    {
        return $this->belongsTo('app\admin\model\User', 'teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function buketeacher()
    {
        return $this->belongsTo('app\admin\model\User', 'buke_teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function student()
    {
        return $this->belongsTo('app\admin\model\user\Student', 'student_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function course()
    {
        return $this->belongsTo('app\admin\model\course\Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function orderGoods()
    {
        return $this->belongsTo('app\admin\model\course\OrderGoods', 'order_goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /*
     * 签到/请假
     * */
    public function sign($orderGoods, $day,$status, $leave_content ="",$is_check = false, $show_exception = false){

        $data = [
            "order_id" => $orderGoods['order_id'],
            "order_goods_id" => $orderGoods['id'],
            "student_id" => $orderGoods['student_id'],
            "course_id" => $orderGoods['course_id'],
            "school_id" => $orderGoods['school_id'],
            "classes_id" => $orderGoods['classes_id'],
            "time_no" => $orderGoods['time_no'],
//            "user_id" => $orderGoods['user_id'],
            "teacher_id" => $orderGoods['teacher_id'],
            "w" => $orderGoods['w'],
//            "class_hour" => $orderGoods['class_hour'],
            "day" => $day,
        ];
        //如果是签到的话 判断是状态
        if(in_array($status,[$this::STATUS_SIGN,$this::STATUS_ADMIN_SIGN])){
            $data['status'] = ['in',[$this::STATUS_SIGN,$this::STATUS_ADMIN_SIGN]];
            $find = $this->where($data)->find();
            if($find){
                $student = Student::get($orderGoods['student_id']);
                $msg = $student['name']."当前课程已经".$find['status_text'];
                exception($msg);
            }
        }

        $data['user_id'] = $orderGoods['user_id'];
        $data['class_hour'] = $orderGoods['class_hour'];

        if($show_exception){
            $condition = [
                "order.id" => $orderGoods['order_id'],
            ];
            $orderModel = new Order();
            $orderInfo = $orderModel->getOrderInfo($condition);
            if($orderInfo['surplus_class_hour'] < $orderGoods['class_hour']){
                exception($orderGoods["student_name"]."的课时不足,"."剩余课时：".$orderInfo['surplus_class_hour']);
            }
        }
        if($is_check){
            if($status == $this::STATUS_SIGN || $this::STATUS_ADMIN_SIGN || $status == $this::STATUS_ACTIVITY|| $status == $this::STATUS_ADMIN){
                $data['status'] = $status;
                $data['createtime'] = time();
                $data['updatetime'] = time();
            }
            return $data;
        }
        switch ($status){
            //请假
            case $this::STATUS_LEAVE:
                $data['status'] = $this::STATUS_LEAVE;
                $data['leave_content'] = $leave_content;
                break;
        }
        $this->save($data);
    }

    /*
     * 请假列表
     * */
    public function askSignList($condition,$is_paginate = 1){
        $list = $this->with(["course"=>function($query){
            $query->withField("name,image");
        },"student"=>function($query){
            $query->withField("name");
        }])->where($condition);
        if($is_paginate){
            $list = $list->paginate();
        }else{
            $list = $list->column("order_sign.student_id,order_sign.status");
            return  $list;
        }

        foreach ($list as $key => $value){
            if(isset($value['course']['image'])){
                $value['course']['image'] = deal_img_url($value['course']['image']);
            }
            $value['createtime_text'] = date("Y-m-d H:i:s",$value['createtime']);
            $value['time_name'] = deal_time_text($value['time_no']);
        }
        return $list;
    }

    /*
     * 签到日志
     * $mode 1 : 日期 2：某一课程某一天的所有人签到记录
     * */
    public function getSignLog($condition,$mode = 1){
        $list =$this->where($condition);
        switch ($mode){
            case 1:
                $list = $list->with(["course"=>function($query){
                    $query->withField("name,image");
                },"teacher"=>function($query){
                    $query->withField("nickname");
                },"school"=>function($query){
                    $query->withField("name");
                }])->group("school_id,classes_id,w,course_id,time_no,day")
                    ->field("day")
                    ->select();
                break;
            case 2:
                $list = $list->with(["student"=>function($query){
                    $query->withField("name");
                },"course"=>function($query){
                    $query->withField("name,image");
                },"teacher"=>function($query){
                    $query->withField("nickname");
                },"school"=>function($query){
                    $query->withField("name");
                }])->select();
                foreach ($list as $key => $value){
                    if(isset($value['course']['image'])){
                        $value['course']['image'] = deal_img_url($value['course']['image']);
                    }
                }
                break;
            default:
        }
        foreach ($list as $key => $value){
            $suffix = substr($value['jilu_image'], strripos($value['jilu_image'], '.') + 1);
            $value['jilu_image_suffix'] = $suffix;
            $value['jilu_image_arr'] = array_filter(explode(",",$value['jilu_image']));


        }
        return $list;
    }


    public static function getTeacherClassHour($teacher_id,$where){
        $count =  self::where($where)
            ->where(function($query) use ($teacher_id){
                $query->where(function ($query1) use ($teacher_id) {
                    $query1->where([
                        "status" => ['in',[self::STATUS_SIGN,self::STATUS_ADMIN_SIGN,self::STATUS_ACTIVITY]],
                        "teacher_id" => $teacher_id
                    ]);
                })->whereOr(function ($query2) use ($teacher_id)  {
                    $query2->where([
                        "status"=>self::STATUS_LEAVE,
                        "is_buke"=>1,
                        "buke_teacher_id"=>$teacher_id,
                    ]);
                });
            })
            ->sum("class_hour");
        return $count;
//        dump(self::getLastSql()); exit;
    }
}
